<?php

/**
 * MyHammer IndexedMemcache
 *
 * LICENSE
 *
 * This source file is subject to the GNU General Public License Version 3
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.myhammer.de/opensource/license/gpl.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to opensource@myhammer.de so we can send you a copy immediately.
 *
 * @category   MyHammer
 * @package    MyHammer_IndexedMemcache
 * @subpackage Importer
 * @license    http://www.myhammer.de/opensource/license/gpl.txt GNU General Public License Version 3
 * @version    1.0
 * @author     Steven Berg <berg@myhammer.de>
 */

class cCsvModelCreater {

    /**
     * create ClassModel from CSV-File
     *
     * @param string $sClassName
     * @param string $sFile
     * @param string $sDelimiter
     * @return string
     */
    public function createModel($sClassName, $sFile, $sDelimiter = ',') {
        $aColumnNames = $this->getColumnNames($sFile, $sDelimiter);
        if (!$aColumnNames) {
            return FALSE;
        }

        $sHeader = $this->createClassHeader($sClassName);
        $sConstants = $this->createConstants($aColumnNames);
        $sConstructor = $this->createConstructor($aColumnNames);
        $sSetterGetter = $this->createSetterAndGetter($aColumnNames);
        $sFooter = $this->createClassFooter();

        $sRetval = $sHeader . $sConstants . $sConstructor . $sSetterGetter . $sFooter;
        return $sRetval;
    }

    /**
     * get ColumnNames
     *
     * @param string $sFile
     * @param string $sDelimiter
     * @return array
     */
    private function getColumnNames($sFile, $sDelimiter) {
        if (!file_exists($sFile)) {
            return FALSE;
        }

        $oFileHandler = fopen ($sFile, 'r');
        if (!$oFileHandler) {
            return FALSE;
        }

        $aColumnNames = fgetcsv($oFileHandler, 4096, $sDelimiter);

        fclose($oFileHandler);

        return $aColumnNames;
    }

    /**
     * create ClassHeader
     *
     * @param string $sClassName
     * @return string
     */
    private function createClassHeader($sClassName) {
        return "\r\n" . 'class ' . $sClassName . ' extends cMemCacheModel {' . "\r\n\r\n";
    }

    /**
     * create ClassFooter
     *
     * @return string
     */
    private function createClassFooter() {
        return '}';
    }

    /**
     * create Constants
     *
     * @param array $aColumns
     * @return string
     */
    private function createConstants($aColumns) {
        $sConst = '';
        foreach ($aColumns as $sColumn) {
            $sConst .= "\t" . 'const ' . strtoupper($sColumn) . ' = ' . "'" . $sColumn . "';\r\n";
        }
        $sConst .= "\r\n";
        return $sConst;
    }

    /**
     * create Constructor
     *
     * @param array $aColumns
     * @return string
     */
    private function createConstructor($aColumns) {
        $sConstructor = "\t" . 'public function __construct() {' . "\r\n";

        foreach ($aColumns as $sColumn) {
            $sConstructor .= "\t\t" . '$this->set' . strtoupper(substr($sColumn, 0, 1)) . substr($sColumn, 1) . '(NULL);' . "\r\n";
        }

        $sConstructor .= "\t" . '}' . "\r\n\r\n";
        return $sConstructor;
    }


    /**
     * create Setter and Getter
     *
     * @param array $aColumn
     * @return string
     */
    private function createSetterAndGetter($aColumn) {
        $sSetter = "\t" . 'public function set';
        $sGetter = "\t" . 'public function get';

        $sRetval = '';

        foreach ($aColumn as $sColumn) {
            $sRetval .= $sSetter . strtoupper(substr($sColumn, 0, 1)) . substr($sColumn, 1) . '($' . $sColumn . ') {' . "\r\n";
            $sRetval .= "\t\t" .  'parent::addData(self::' . strtoupper($sColumn) . ', $' . $sColumn . ');' . "\r\n";
            $sRetval .= "\t" . '}' . "\r\n\r\n";

            $sRetval .= $sGetter . strtoupper(substr($sColumn, 0, 1)) . substr($sColumn, 1) . '() {' . "\r\n";
            $sRetval .= "\t\t" . 'return parent::getData(self::' . strtoupper($sColumn) . ');' . "\r\n";
            $sRetval .= "\t" .  '}' . "\r\n\r\n";
        }

        return $sRetval;
    }
}